package com.taskcollection.servlet;

import com.taskcollection.bean.ProjectProgressBean;
import com.taskcollection.bean.StudentBean;
import com.taskcollection.service.ProjectProgressService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by haocongshun on 2016/8/4.
 */
@WebServlet(name = "ProjectProgressServlet")
public class ProjectProgressServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StudentBean stu = (StudentBean) request.getSession().getAttribute("学生");
        DiskFileItemFactory factory=new DiskFileItemFactory();
        ServletFileUpload upload=new ServletFileUpload(factory);
        try{
            Map<String ,String > result = new HashedMap();
            List<FileItem> items = upload.parseRequest(request);
            for(FileItem item : items){
                if(!item.isFormField()){
                    String path = "/static/upload";
                    path=request.getSession().getServletContext().getRealPath(path);
                    String filename =item.getName();//原始文件名
                    filename=(new Date()).getTime()+"_" + filename; //文件重命名
                    System.out.println("upload Path:"+(path+ File.separator+filename));
                    FileOutputStream os = new FileOutputStream(path+ File.separator+filename);
                    IOUtils.copy(item.getInputStream(),os);
                    os.flush();
                    os.close();
                    result.put(item.getFieldName(),"/static/upload/"+filename);
                }else {
                    result.put(item.getFieldName(), IOUtils.toString(item.getInputStream(),"utf-8"));
                }
            }
            ProjectProgressService pps = new ProjectProgressService();
            if(pps.add(result,stu.getStudent_id())){
                request.getSession().setAttribute("msg","提交成功！");
                //request.getRequestDispatcher("/student/projectProgress.jsp").forward(request,response);
                response.sendRedirect("/TaskCollection/student/projectProgress.jsp");
            }else {
                request.getSession().setAttribute("msg","提交失败！");
                response.sendRedirect("/TaskCollection/student/projectProgress.jsp");
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
    }
}
